package com.kanas.webak.config.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.config.domain.I18nResource;
import com.kanas.webak.config.service.I18nResourceService;

@Controller
@RequestMapping("app/config/i18nresource")
public class I18nResourceController extends ControllerSupport {

	private I18nResourceService i18nResourceService;

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView properties(){
		ModelAndView mv=new ModelAndView();
		mv.addObject("i18ns", i18nResourceService.findAll());
		mv.setViewName("modules/sysadmin/i18n");
		return mv;
	}
	
	@RequestMapping(value="/new.html")
	public ModelAndView addNew(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/config/i18n_resource_new");
		return mv;
	}
	
	@RequestMapping("new.do")
	public @ResponseBody ResponseMessage addNew(final I18nResource i18n) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return i18nResourceService.addNew(i18n);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/edit/{dictid}.html")
	public ModelAndView groupEditView(@PathVariable("dictid")String did){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/config/i18n_resource_edit");
		mv.addObject("dict", i18nResourceService.findOne(did));
		return mv;
	}
	
	@RequestMapping("update/edit.do")
	public @ResponseBody ResponseMessage update(final I18nResource i18n) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return i18nResourceService.update(i18n);
			}
		}, logger);
		return response;
	}
	
	public I18nResourceService getI18nResourceService() {
		return i18nResourceService;
	}

	@Resource(name="com.kanas.webak.config.service.i18nresourceservice")
	public void setI18nResourceService(I18nResourceService i18nResourceService) {
		this.i18nResourceService = i18nResourceService;
	}
}
